package com.han.fu.api.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.han.fu.api.business.model.ao.*;
import com.han.fu.api.business.model.vo.NewsBrowseVo;
import com.han.fu.api.business.model.vo.NewsInfoVo;
import com.han.fu.api.business.model.vo.NewsVo;
import com.han.fu.api.business.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 新闻接口
 *
 * @author: star
 * @date: 2021-08-05
 */
@RestController
public class NewsController {

    @Autowired
    INewsService iNewsService;

    /**
     * 新闻分页查询(用户端使用)
     *
     * @param ao 分页查询条件
     * @return Page<NewsBrowseVo> 分页数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/news/browse/page")
    public Page<NewsBrowseVo> queryBrowsePage(@Validated @RequestBody NewsBrowseAo ao) {
        return iNewsService.queryBrowsePage(ao);
    }


    /**
     * 新闻分页查询(后台端使用)
     *
     * @param ao 分页查询条件
     * @return Page<NewsVo> 分页数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/news/page")
    public Page<NewsVo> queryNewsPage(@Validated @RequestBody NewsPageAo ao) {
        return iNewsService.queryNewsPage(ao);
    }

    /**
     * 新闻明细查询
     *
     * @param id 主键
     * @return NewsInfoVo 明细
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @GetMapping("/news/detail")
    public NewsInfoVo queryNewsDetail(@NotNull(message = "ID不能为空") @RequestParam String id) {
        return iNewsService.queryNewsDetail(id);
    }

    /**
     * 新闻新增数据
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/news/add")
    public void addNews(@Validated @RequestBody NewsAddAo ao) {
        iNewsService.addNews(ao);
    }

    /**
     * 新闻修改数据
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/news/update")
    public void updateNews(@Validated @RequestBody NewsUpdateAo ao) {
        iNewsService.updateNews(ao);
    }

    /**
     * 新闻删除数据
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/news/delete")
    public void deleteNews(@Validated @RequestBody NewsDeleteAo ao) {
        iNewsService.deleteNews(ao);
    }

    /**
     * 新闻审批数据
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('admin')")
    @PostMapping("/news/audit")
    public void auditNews(@Validated @RequestBody NewsAuditAo ao) {
        iNewsService.auditNews(ao);
    }

}